Skip to content

Conversation

@bensheldon
Copy link
Contributor

@bensheldon bensheldon commented Mar 11, 2024

This fixes an inconsistency in behavior in ThreadPoolExecutor in Ruby and JRuby.

Extracted from #1042, explained in #1041.

Changing the behavior of kill to wait until all threads exited exposed a problem with TimerSet's timer thread. It was not previously being completely shut down. The change was to ensure that the looped Event.wait is released with Event#set on shutdown.

One wrinkle in TimerSet is that shutdown is wrapped with a mutex that also is locked in the timer thread, which could lead to a deadlock: shutdown takes mutex and waits for timer thread to exit; meanwhile the timer thread is itself trying to acquire the mutex in order to execute through and exit... therefore deadlock. My solution to this was moving the timer thread's kill / await-thread-termination outside of the mutex.

This deadlock was largely masked because JRuby wasn't waiting on the threads to exit (changed in this PR) whereas CRuby would simply kill the threads so they would have a chance to deadlock on the mutex.

@bensheldon bensheldon added the bug A bug in the library or documentation. label Dec 10, 2025
@bensheldon bensheldon changed the title ThreadPoolExecutor kill will wait_for_termination in JRuby ThreadPoolExecutor kill should wait_for_termination in JRuby Dec 10, 2025
@bensheldon bensheldon changed the title ThreadPoolExecutor kill should wait_for_termination in JRuby ThreadPoolExecutor kill should wait_for_termination in JRuby to be consistent with Ruby Dec 10, 2025
…e TimerSet timer thread shuts down cleanly
@bensheldon bensheldon changed the title ThreadPoolExecutor kill should wait_for_termination in JRuby to be consistent with Ruby ThreadPoolExecutor#kill will wait_for_termination in JRuby; ensure TimerSet timer thread shuts down cleanly Dec 11, 2025
@bensheldon bensheldon changed the title ThreadPoolExecutor#kill will wait_for_termination in JRuby; ensure TimerSet timer thread shuts down cleanly ThreadPoolExecutor#kill will wait_for_termination in JRuby; ensure TimerSet timer thread shuts down cleanly Dec 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug A bug in the library or documentation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant